package com.eight.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.dao.PermissionMapper;
import com.eight.pojo.SysMenu;
import com.eight.service.PermissionService;
import com.eight.util.JWTTool;
import com.eight.util.MenuLeveUtil;
import com.eight.util.WebUtil;
import com.eight.vo.MenuVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.List;
import java.util.Map;

/**
 * @Description: 业务层的实现类模板
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-23  17:21
 * @Version: 1.0
 */
@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, SysMenu> implements PermissionService  {
    @Resource(name = "permissionMapper")
    private PermissionMapper permissionMapper;

    /**
     * 根据用户名字查询用户权限
     * @param name 用户名字
     * @return 查询到的权限List
     */
    @Override
    public List<SysMenu> getPermissionByName(String name){
        return permissionMapper.getPermissionByName(name);
    }

    /**
     * 从token 获取 当前登录者的名字 来进行查询
     * @return 返回处理过的权限 三级菜单
     */
    @Override
    public List<MenuVo> getPermissionByTokenName() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JWTTool.analyzeToken(token);
        String username = (String) map.get("username");
        List<MenuVo> menuVos = MenuLeveUtil.MakeMenuVo(permissionMapper.getPermissionByTokenName(username));
        List<MenuVo> menuVos1 = MenuLeveUtil.MakeLayersMenuVo(menuVos);
        return menuVos1;
    }

    /**
     * 返回经过菜单筛选的菜单
     * @return 处理过的结果
     */
    @Override
    public List<SysMenu> getPermissionAll() {
        return MenuLeveUtil.MakeLayersMenu(permissionMapper.selectList(new QueryWrapper<>()));
    }

    /**
     * 查询所选角色所有权限id
     * @param roleId 角色id
     * @return 返回所有权限的id
     */
    @Override
    public List<Integer> getPermissionByRid(int roleId) {
        return permissionMapper.selectPermissionByRid(roleId);
    }

    /**
     * 根绝权限id修改权限
     * @param sysMenu 被修改全新啊的新信息
     * @return 返回修改结果
     */
    @Override
    public Integer updatePermission(SysMenu sysMenu) {
        return permissionMapper.updateById(sysMenu);
    }

}
